import { createRouter, createWebHistory } from 'vue-router';
import Login from '../components/Login.vue';
import ModelProviders from '../components/ModelProviders.vue';
import Home from '../components/Home.vue';
import OrdinaryEmployeeView from '../components/views/OrdinaryEmployeeView.vue';
import FinanceApproverView from '../components/views/FinanceApproverView.vue';
import SystemManagerView from '../components/views/SystemManagerView.vue';
import ModelProviderManagement from '../views/ModelProviderManagement.vue'


const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login,
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    },
    {
        path: '/model-providers',
        name: 'ModelProviders',
        component: ModelProviders,
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
    },
     {
        path: '/ordinary-employee',
        name: 'OrdinaryEmployee',
        component: OrdinaryEmployeeView
      },
      {
        path: '/finance-approver',
        name: 'FinanceApprover',
        component: FinanceApproverView
      },
     // {
     //    path: '/system-manager',
     //    name: 'SystemManagerView',
     //     component: SystemManagerView,
     // },
     {
        path: '/model-providers',
        name: 'ModelProviderManagement', // 必须与菜单项的name一致
        component: ModelProviderManagement
      },{
          path: '/system-manager',
          name: 'SystemManagerView', // 父级路由名称
          component: SystemManagerView,
          children: [
                {
                  path: 'model-provider-management', // 注意这里不需要重复写 /system-config
                  name: 'ModelProviderManagement', // 必须与菜单的name属性一致
                  component: ModelProviderManagement
                }
            ]
     }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 全局导航守卫
router.beforeEach((to, from, next) => {
    // 假设从 localStorage 中获取登录状态
    const isLoggedIn = localStorage.getItem('isLoggedIn');
    console.log('isLoggedIn:', isLoggedIn); // 添加这行调试语句
    // 检查用户是否已登录
    if (to.name!== 'Login' &&!isLoggedIn) {
        next({ name: 'Login' }); // 如果未登录，重定向到登录页面
    } else {
        next(); // 继续导航
    }
});

export default router;